﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WorldOfSuperpowers_COMMON.DataModel;

public class TransformNetSender : MonoBehaviour
{
    public ushort RoleID;

    private bool SyncPos;

    private bool SyncRotation;

    private bool SyncAnim;
    private int[] MoveAnimAxis;
    private Transform MyTran;
    private AnimController AnimC;

    /// <summary>
    /// 同步初始化
    /// </summary>
    public void InitSender(bool pos,bool rotation,bool anim,AnimController _AnimC,ushort _roleID)
    {
        RoleID = _roleID;
        SyncAnim = anim;
        SyncPos = pos;
        SyncRotation = rotation;
        MyTran = transform;
        AnimC = _AnimC;
        AnimC.MoveAnimSenderAction = UpdateAnim;
        //开始同步位置信息
        InvokeRepeating("UpdatePos", 0,0.01f);
    }

    /// <summary>
    /// 同步位置
    /// </summary>
    public void UpdatePos()
    {
        int x = 0;
        int y = 0;
        int z = 0;
        UpdateMoveModel model = new UpdateMoveModel();
        model.RoleID = RoleID;
        if (SyncPos)
        {
            //获取当前位置
            x = NumTools.GetInstance().GetIntFromFloat(MyTran.position.x);
            y = NumTools.GetInstance().GetIntFromFloat(MyTran.position.y);
            z = NumTools.GetInstance().GetIntFromFloat(MyTran.position.z);
            model.posArr = new[] {x, y, z};
        }
        if (SyncRotation)
        {
            //获取当前方向
            x = NumTools.GetInstance().GetIntFromFloat(MyTran.eulerAngles.x);
            y = NumTools.GetInstance().GetIntFromFloat(MyTran.eulerAngles.y);
            z = NumTools.GetInstance().GetIntFromFloat(MyTran.eulerAngles.z);
            model.dirArr = new[] {x, y, z};
        }
        //获取动画信息
        if (SyncAnim)
        {
            model.moveAnimAxis = MoveAnimAxis;
        }
        //todo 塞入同步包内发送给服务器
        
    }

    /// <summary>
    /// 保存移动动画轴值
    /// </summary>
    public void UpdateAnim(Vector2 _MoveAnimAxis)
    {
        int x = NumTools.GetInstance().GetIntFromFloat(_MoveAnimAxis.x);
        int y = NumTools.GetInstance().GetIntFromFloat(_MoveAnimAxis.y);
        MoveAnimAxis = new int[] { x, y };
    }
}
